// pages/my/my-orderDesc/my-orderDesc.js
var request = require('../../../utils/request.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderType: options.curId,
      id: options.id,
    })
  
  },

  getToken: function () {
    console.log(getCurrentPages())
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        request.getOrderDetail(
          {
            "session_id": tempToken,
            "id": that.data.id
          },
          (res) => {
            console.log(res);
            if (res.data.status == "success") {
              that.setData({
                orderDetail: res.data.data
              })

            } else {
              wx.showToast({
                title: '请求失败',
                image: "../../../images/xx.png"
              })
            }
            
          }
        )

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  onShow: function () {
    var that = this;

    that.getToken();

  },
  //去还书
  
  goReturnBook: function () {
    wx.redirectTo({
      url: '../my-returnBook/my-returnBook',
    })
  },

  //去支付
  goWallet: function () {
    console.log(getCurrentPages())
    var that = this;

    var postData = {
      "session_id": that.data.token,
      "id": that.data.orderDetail.id,
      "money": that.data.orderDetail.money
    };
    console.log(postData)
    request.reduceBalance(
      postData,
      (res) => {
        console.log(res);
        if (res.data.status == "success") {
          wx.showToast({
            title: "支付成功",
            icon: "success",
            success: function () {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
            confirmColor: "#ff0000",
          })
        }
      }
    )


  },
  //去维修页面
  gofaultRepair: function (e) {
    // wx.navigateTo({
    //   url: '../my-faultRepair/my-faultRepair',
    // })
    var that = this;
    var postData = {
      "session_id": that.data.token,
      "id": e.currentTarget.dataset.id
    };

    console.log(postData)
    request.getLostBook(postData,
      (res)=>{
        console.log(res)
        wx.showModal({
          title: '',
          content: res.data.msg,
          showCancel:false,
          confirmColor: "#ff0000",
          success:function(res){
            wx.navigateBack({
              delta:1
            })
          }
        })
      }
    )
  }

})








